package com.hongxun.modules.bss.controller;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.hongxun.common.utils.*;
import com.hongxun.common.validator.ValidatorUtils;
import com.hongxun.common.validator.group.AddGroup;
import com.hongxun.common.validator.group.UpdateGroup;
import com.hongxun.config.PriceConfig;
import com.hongxun.modules.api.entity.BEstimateEntity;
import com.hongxun.modules.api.entity.BOtherCostEntity;
import com.hongxun.modules.api.entity.BProjectApplicationEntity;
import com.hongxun.modules.api.service.BDataDictionaryService;
import com.hongxun.modules.api.service.BEstimateService;
import com.hongxun.modules.api.service.BOtherCostService;
import com.hongxun.modules.api.service.BProjectApplicationService;
import com.hongxun.modules.bss.entity.*;
import com.hongxun.modules.bss.service.*;
import com.hongxun.modules.csg.entity.CPBaseEntity;
import com.hongxun.modules.csg.entity.CScheduleEntity;
import com.hongxun.modules.csg.service.CPBaseService;
import com.hongxun.modules.csg.service.CScheduleService;
import com.hongxun.modules.eys.entity.*;
import com.hongxun.modules.eys.service.*;
import com.hongxun.modules.sys.controller.AbstractController;
import com.hongxun.modules.sys.entity.SysDeptEntity;
import com.hongxun.modules.sys.entity.SysRoleEntity;
import com.hongxun.modules.sys.entity.SysUserEntity;
import com.hongxun.modules.sys.service.*;
import com.lowagie.text.*;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 导出结果和购置表及拆除表Controller
 *
 * @author zkcn
 * @email youeremail@163.com
 * @date 2020-2-7 17:51:07
 */
@RestController
@RequestMapping("/bss/batchDown")
public class BatchDownController extends AbstractController {
    @Value("${com.hongxun.IMAGEROTT}")
    private String ROOT;
    @Autowired
    private APBaseService aPBaseService;
    @Autowired
    private APOtherService aPOtherService;
    @Autowired
    private APEstimateService aPEstimateService;
    @Autowired
    private APCostService aPCostService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private APCriteriaService aPCriteriaService;
    @Autowired
    private DDeviceBuyService dDeviceBuyService;
    @Autowired
    private DDeviceOldService dDeviceOldService;
    @Autowired
    private BDataDictionaryService bDataDictionaryService;
    @Autowired
    private DMaterialBuyService dMaterialBuyService;
    @Autowired
    private DMaterialOldService dMaterialOldService;
    @Autowired
    private DPictureService dPictureService;
    @Autowired
    private DPointService dPointService;
    @Autowired
    private DCrossCostService dCrossCostService;
    @Autowired
    private APRangeService aPRangeService;
    @Autowired
    private DWorkContentService dWorkContentService;


    /**
     * 购置表和拆除表
     *
     * @param params
     * @return
     */
    @RequestMapping("/buyOrOld")
    public R getVdate(@RequestParam Map<String, Object> params) {
        String guid = params.get("guid").toString();
        APBaseEntity aPBase = aPBaseService.queryObject(guid);
        Map<String, Object> mapaPOther = new HashMap<>();

        //  设备购置
        List<DDeviceBuyEntity> dDeviceBuyList = dDeviceBuyService.queryListout(aPBase.getGuid());
        List<BuyEntityOut> dDeviceBuyOutList = new ArrayList<BuyEntityOut>();
        for (DDeviceBuyEntity dDeviceBuyEntity : dDeviceBuyList) {
            BuyEntityOut buyEntityOut = new BuyEntityOut();
            buyEntityOut.setDBMidtype(dDeviceBuyEntity.getDBMidtype());
            buyEntityOut.setDBName(dDeviceBuyEntity.getDBName());
            buyEntityOut.setDBUnit(dDeviceBuyEntity.getDBUnit());
            buyEntityOut.setDBCount(dDeviceBuyEntity.getDBCount() == null ? "0" : dDeviceBuyEntity.getDBCount().toString());
            buyEntityOut.setDBPrice(dDeviceBuyEntity.getDBPrice() == null ? "0.00" : dDeviceBuyEntity.getDBPrice().toPlainString());
            buyEntityOut.setDBPriceall(dDeviceBuyEntity.getDBPriceall() == null ? "0.00" : dDeviceBuyEntity.getDBPriceall().toPlainString());
            dDeviceBuyOutList.add(buyEntityOut);
        }

        //  材料购置
        List<DMaterialBuyEntity> dMaterialBuyList = dMaterialBuyService.queryListout(aPBase.getGuid());
        List<BuyEntityOut> dMaterialBuyOutList = new ArrayList<BuyEntityOut>();
        for (DMaterialBuyEntity dMaterialBuyEntity : dMaterialBuyList) {
            BuyEntityOut buyEntityOut = new BuyEntityOut();
            buyEntityOut.setDBMidtype(dMaterialBuyEntity.getMBMidtype());
            buyEntityOut.setDBName(dMaterialBuyEntity.getMBName());
            buyEntityOut.setDBUnit(dMaterialBuyEntity.getMBUnit());
            buyEntityOut.setDBCount(dMaterialBuyEntity.getMBCount() == null ? "0" : dMaterialBuyEntity.getMBCount().toString());
            buyEntityOut.setDBPrice(dMaterialBuyEntity.getMBPrice() == null ? "0.00" : dMaterialBuyEntity.getMBPrice().toPlainString());
            buyEntityOut.setDBPriceall(dMaterialBuyEntity.getMBPriceall() == null ? "0.00" : dMaterialBuyEntity.getMBPriceall().toPlainString());
            dMaterialBuyOutList.add(buyEntityOut);
        }

        //  拆除设备
        String dDeviceOldwheres = " 1=1 ";
        dDeviceOldwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        dDeviceOldwheres += " and  d_r_datastate != '删除' ";
        mapaPOther.put("filterSql", dDeviceOldwheres + " ");
        List<DDeviceOldEntity> dDeviceOldList = dDeviceOldService.queryList(mapaPOther);
        List<DDeviceOldEntityOut> dDeviceOldOutList = new ArrayList<DDeviceOldEntityOut>();
        for (DDeviceOldEntity dDeviceOldEntity : dDeviceOldList) {
            DDeviceOldEntityOut dDeviceOldEntityOut = new DDeviceOldEntityOut();
            dDeviceOldEntityOut.setDRMidtype(dDeviceOldEntity.getDRMidtype());
            dDeviceOldEntityOut.setDRUnit("套");
            dDeviceOldEntityOut.setDRCount(new BigDecimal(1.00));
            dDeviceOldEntityOut.setDRName(dDeviceOldEntity.getDRName());
            //  处置意见
            if (dDeviceOldEntity.getDROpinion() != null && dDeviceOldEntity.getDROpinion().equals("-9")) {
                dDeviceOldEntityOut.setDROpinion("本项目利用");
            } else {
                dDeviceOldEntityOut.setDROpinion(bDataDictionaryService.selectvaluebykey("opinion_type", dDeviceOldEntity.getDROpinion()));
            }

            //  处置去向
            if (dDeviceOldEntity.getDRWhere() != null) {
                dDeviceOldEntityOut.setDRWhere(bDataDictionaryService.selectvaluebykey("where_type", dDeviceOldEntity.getDROpinion()));
            }

            dDeviceOldOutList.add(dDeviceOldEntityOut);
        }

        //  拆除材料
        mapaPOther.clear();
        String dMaterialOldwheres = " 1=1 ";
        dMaterialOldwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        dMaterialOldwheres += " and  m_b_datastate != '删除' ";
        mapaPOther.put("filterSql", dMaterialOldwheres + " ");
        List<DMaterialOldEntity> dMaterialOldEntityList = dMaterialOldService.queryList(mapaPOther);
        List<DDeviceOldEntityOut> dMaterialOldOutList = new ArrayList<>();
        for (DMaterialOldEntity dMaterialOldEntity : dMaterialOldEntityList) {
            DDeviceOldEntityOut dDeviceOldEntityOut = new DDeviceOldEntityOut();
            dDeviceOldEntityOut.setDRMidtype(dMaterialOldEntity.getMBMidtype());
            dDeviceOldEntityOut.setDRName(dMaterialOldEntity.getMBName());
            dDeviceOldEntityOut.setDRUnit(dMaterialOldEntity.getMBUnit());
            dDeviceOldEntityOut.setDRCount(dMaterialOldEntity.getMBCount());
            dDeviceOldEntityOut.setDROpinion(bDataDictionaryService.selectvaluebykey("opinion_type", dMaterialOldEntity.getDROpinion()));
            dMaterialOldOutList.add(dDeviceOldEntityOut);
        }


        return R.ok()
                .put("dMaterialOldList", dMaterialOldOutList)
                .put("dDeviceOldList", dDeviceOldOutList)
                .put("dMaterialBuyList", dMaterialBuyOutList)
                .put("dDeviceBuyList", dDeviceBuyOutList);

    }


    /**
     * 导出结果一键下载
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws DocumentException
     */
    @RequestMapping("/batchDown")
    public void batchDown(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {

        String guid = request.getParameter("guid");
        String fjguid = request.getParameter("fjguid");
        APBaseEntity aPBase = aPBaseService.queryObject(guid);
        String rootPath = request.getSession().getServletContext().getRealPath("/");

        File folder = new File(rootPath + "/" + aPBase.getPBName());
        if (!folder.exists()) {
            folder.mkdirs();
        }

        String[] fjArr = fjguid.split(",");
        FileOutputStream out = null;
        for (String s : fjArr) {

            if (s.equals("fj1")) {        //	申请书
                String filename1 = aPBase.getPBBookname() + ".doc";
                out = new FileOutputStream(folder + "/" + filename1);
                exports(guid, out);
            } else if (s.equals("fj2")) {    //	材料设备购置表
                String filename2 = aPBase.getPBName() + "材料设备购置表.xls";
                out = new FileOutputStream(folder + "/" + filename2);
                exportsbymx(guid, out);
            } else if (s.equals("fj3")) {    //	材料设备拆除表
                String fileRemove = aPBase.getPBName() + "材料设备拆除表.xls";
                out = new FileOutputStream(folder + "/" + fileRemove);
                exportRemove(guid, out);
            } else if (s.equals("fj4")) {    //	点位材料设备明细表
                String filename4 = aPBase.getPBName() + "点位设备材料明细表.xls";
                out = new FileOutputStream(folder + "/" + filename4);
                exportsbypoint(guid, out);
            } else if (s.equals("fj5")) {    //	材料汇总表
                String filename3 = aPBase.getPBName() + "材料设备汇总表.xls";
                out = new FileOutputStream(folder + "/" + filename3);
                exportsbyhz(guid, out);
            } else {                        //	点位照片表
                String filename5 = aPBase.getPBName() + "项目照片.doc";
                out = new FileOutputStream(folder + "/" + filename5);
                exportsimg(guid, filename5, out);
            }
        }

        String zipFrontFile = aPBase.getPBName() + ".zip";
        zipFrontFile = URLEncoder.encode(zipFrontFile, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + zipFrontFile);
        response.setContentType("application/zip");
        response.setStatus(200);
        ZipFilesUtil.toZip(folder.getPath(), response.getOutputStream(), true);
        delFile(folder);

    }


    /**
     * 点位照片表
     *
     * @param
     * @param
     * @throws IOException
     * @throws DocumentException
     */
    @RequestMapping("/exportsimg")
    public void exportsimg(String guid, String filename, FileOutputStream out) throws IOException, DocumentException {
        APBaseEntity aPBase = aPBaseService.queryObject(guid);

        Document document = new Document();
        BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        RtfWriter2.getInstance(document, out);
        Font titleFontye = new Font(bfChinese, 8, Font.BOLD);
        HeaderFooter footer = new HeaderFooter(new Phrase("页码：", titleFontye), true);
        footer.setAlignment(1);
        footer.setBorder(Rectangle.NO_BORDER);

        document.setFooter(footer);
        document.open();
        setexportsimg(document, bfChinese, aPBase);

        document.close();
    }

    public void setexportsimg(Document document, BaseFont bfChinese, APBaseEntity aPBase) throws DocumentException, IOException {


        //表题字体
        Font titleFont = new Font(bfChinese, 11, Font.BOLD);
        //表格字体
        Font contextFont = new Font(bfChinese, 8, Font.NORMAL);
        // 签字字体
        Font qinFont = new Font(bfChinese, 10, Font.NORMAL);
        Paragraph title = new Paragraph((aPBase.getPBName() == null ? "" : aPBase.getPBName()) + "项目照片");
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(titleFont);
        document.add(title);

        Map<String, Object> mapapole = new HashMap<>();
        String polewheres = " 1=1 ";
        polewheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        polewheres += " and  p_p_datastate!= '删除' ";
       mapapole.put("filterSql", polewheres + " order by p_r_guid,   CONVERT(Right(p_p_code,LENGTH(p_p_code)-instr(p_p_code, '#')) ,SIGNED) asc ");
        List<DPointEntity> dPointList = dPointService.queryList(mapapole);

        Map<String, Object> mapCost = new HashMap<>();
        String mapCostwheres = " 1=1 ";
        mapCostwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        mapCostwheres += " and  c_c_datastate!= '删除' ";
        mapCost.put("filterSql", mapCostwheres + " ");
        List<DCrossCostEntity> dCrossCostList = dCrossCostService.queryList(mapCost);

        addpoleimg(document, aPBase, contextFont, dPointList);
        addcrossimg(document, aPBase, contextFont, dCrossCostList);

    }

    private void addcrossimg(Document document, APBaseEntity aPBase, Font contextFont, List<DCrossCostEntity> dCrossCostList) throws DocumentException, IOException {
        for (DCrossCostEntity dCrossCostEntity : dCrossCostList) {
            Map<String, Object> mapaPOther = new HashMap<>();
            String mapaPOtherwheres = " 1=1 ";
            mapaPOtherwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
            mapaPOtherwheres += " and  p_status = '储备' ";
            mapaPOtherwheres += " and  p_p_guid = '" + dCrossCostEntity.getGuid().toString() + "' ";
            mapaPOtherwheres += " and  p_datastate != '删除' ";
            mapaPOther.put("filterSql", mapaPOtherwheres + " ");
            List<DPictureEntity> dPictureList = dPictureService.queryList(mapaPOther);
//			document.setMargins(40F,40F,50F,90F);	//	文档边距
            document.setMargins(40F, 40F, 20F, 73F);    //	文档边距
            if (dPictureList.size() > 0) {

                Table table = new Table(6);
                int[] withs = {1, 2, 1, 2, 1, 2};
                table.setWidths(withs);
                /** 表格所占页面宽度 author:yyli Sep 15, 2010 */
                table.setWidth(90);
                /** 居中显示 author:yyli Sep 15, 2010 */
                table.setAlignment(Element.ALIGN_CENTER);
                /** 自动填满 author:yyli Sep 15, 2010 */
                table.setAutoFillEmptyCells(true);
                APRangeEntity aPRangeEntity = aPRangeService.queryObject(dCrossCostEntity.getPRGuid());

                OutPutUtils.addcell(table, bDataDictionaryService.selectvaluebykey("cross_type", dCrossCostEntity.getCType()) + "(" + aPRangeEntity.getPRName() + "_" + dCrossCostEntity.getCCName() + ")", 0, 6, true, contextFont);
                //	一个点位最多显示3张照片
                int chushu = 1;
                /*int chishu = dPictureList.size() / 3;
				int yushu = dPictureList.size() % 3;

				if (yushu > 0) {
					chishu = chishu + 1;
				}*/
                for (int i = 0; i < chushu; i++) {
                    boolean one = false;
                    boolean two = false;
                    boolean three = false;

                    if ((3 * i) < dPictureList.size()) {
                        one = true;
                    }
                    if (((3 * i) + 1) < dPictureList.size()) {
                        two = true;
                    }
                    if (((3 * i) + 2) < dPictureList.size()) {
                        three = true;
                    }
                    if (one) {
                        File file = new File(ROOT + dPictureList.get(3 * i).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get(3 * i).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }
                    if (two) {
                        File file = new File(ROOT + dPictureList.get((3 * i) + 1).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 1).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }
                    if (three) {
                        File file = new File(ROOT + dPictureList.get((3 * i) + 2).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 2).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }


                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }

                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPTime() == null ? "" : DateUtils.format(dPictureList.get(3 * i).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 1).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 2).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }

                }

                Paragraph nuss = new Paragraph("");    //	表与表之间加空行（调整格式）
                document.add(nuss);
                document.add(table);
            }
        }
    }

    private void addpoleimg(Document document, APBaseEntity aPBase, Font contextFont, List<DPointEntity> dPointList) throws DocumentException, IOException {
        for (DPointEntity dPointEntity : dPointList) {
            Map<String, Object> mapaPOther = new HashMap<>();
            String mapaPOtherwheres = " 1=1 ";
            mapaPOtherwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
            mapaPOtherwheres += " and  p_status = '储备' ";
            mapaPOtherwheres += " and  p_p_guid = '" + dPointEntity.getGuid().toString() + "' ";
            mapaPOtherwheres += " and  p_datastate != '删除' ";
            mapaPOther.put("filterSql", mapaPOtherwheres + " ");
            List<DPictureEntity> dPictureList = dPictureService.queryList(mapaPOther);
            document.setMargins(40F, 40F, 20F, 73F);    //	文档边距
            if (dPictureList.size() > 0) {

                Table table = new Table(6);
                int[] withs = {1, 2, 1, 2, 1, 2};
                table.setWidths(withs);
                /** 表格所占页面宽度 author:yyli Sep 15, 2010 */
                table.setWidth(90);

                /** 居中显示 author:yyli Sep 15, 2010 */
                table.setAlignment(Element.ALIGN_CENTER);
                /** 自动填满 author:yyli Sep 15, 2010 */
                table.setAutoFillEmptyCells(true);
                APRangeEntity aPRangeEntity = aPRangeService.queryObject(dPointEntity.getPRGuid());

                OutPutUtils.addcell(table, dPointEntity.getPPCode() + "(" + aPRangeEntity.getPRName() + "_" + dPointEntity.getPPName() + ")", 0, 6, true, contextFont);

                //	一个点位设定最多显示3张照片
				/*int chishu = dPictureList.size() / 3;
				int yushu = dPictureList.size() % 3;

				if (yushu > 0) {
					chishu = chishu + 1;
				}*/

                int chushu = 1;
                for (int i = 0; i < chushu; i++) {
                    boolean one = false;
                    boolean two = false;
                    boolean three = false;

                    if ((3 * i) < dPictureList.size()) {
                        one = true;
                    }
                    if (((3 * i) + 1) < dPictureList.size()) {
                        two = true;
                    }
                    if (((3 * i) + 2) < dPictureList.size()) {
                        three = true;
                    }
                    if (one) {
                        File file = new File(ROOT + dPictureList.get(3 * i).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get(3 * i).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }
                    if (two) {
                        File file = new File(ROOT + dPictureList.get((3 * i) + 1).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 1).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }
                    if (three) {
                        File file = new File(ROOT + dPictureList.get((3 * i) + 2).getPServeraddress());
                        if (file.exists()) {
                            OutPutUtils.addcellimg(table, ROOT + dPictureList.get((3 * i) + 2).getPServeraddress(), 8, 2, true, contextFont);
                        } else {
                            OutPutUtils.addcell(table, "没找到照片", 8, 2, true, contextFont);

                        }
                    } else {
                        OutPutUtils.addcell(table, "", 8, 2, true, contextFont);
                    }


                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPLabel(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }

                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "来源", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPPResour(), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }

                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (one) {
                        OutPutUtils.addcell(table, dPictureList.get(3 * i).getPTime() == null ? "" : DateUtils.format(dPictureList.get(3 * i).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (two) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 1).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 1).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }
                    OutPutUtils.addcell(table, "时间", 0, 0, true, contextFont);
                    if (three) {
                        OutPutUtils.addcell(table, dPictureList.get((3 * i) + 2).getPTime() == null ? "" : DateUtils.format(dPictureList.get((3 * i) + 2).getPTime(), "yyyy-MM-dd"), 0, 0, true, contextFont);
                    } else {
                        OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    }

                }
                Paragraph nuss = new Paragraph("");    //	表与表之间加空行（调整格式）
                document.add(nuss);
                document.add(table);
            }
        }
    }


    /**
     * 申请书
     *
     * @param
     * @param
     * @throws IOException
     * @throws DocumentException
     */
    @RequestMapping("/exports")
    public void exports(String guid, FileOutputStream out) throws IOException, DocumentException {
        APBaseEntity aPBase = aPBaseService.queryObject(guid);

        Document document = new Document(PageSize.A4.rotate());
        BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        RtfWriter2.getInstance(document, out);
        Font titleFontye = new Font(bfChinese, 8, Font.BOLD);
        HeaderFooter footer = new HeaderFooter(new Phrase("页码：", titleFontye), true);
        footer.setAlignment(1);
        footer.setBorder(Rectangle.NO_BORDER);
        document.setFooter(footer);
        document.open();
        setexports(document, bfChinese, aPBase);

        document.close();

    }

    /**
     * 导出材料汇总
     *
     * @param
     * @param
     * @throws IOException
     * @throws DocumentException
     */
    @RequestMapping("/exportsbyhz")
    public void exportsbyhz(String guid, FileOutputStream out) throws IOException, DocumentException {

        try {
            APBaseEntity aPBase = aPBaseService.queryObject(guid);
            List<DMaterialBuyEntity> dMaterialBuyList = dMaterialBuyService.queryListhz(aPBase.getGuid());
            List<DDeviceBuyEntity> dDeviceBuyList = dDeviceBuyService.queryListhz(aPBase.getGuid());

            String[] title = {"类别", "名称", "单位", "数量", "价格"};
//            String fileName = aPBase.getPBName() + "材料汇总表" + System.currentTimeMillis() + ".xls";
            String sheetName = "材料设备汇总表";
            String[][] content = new String[dMaterialBuyList.size() + dDeviceBuyList.size()][title.length];
            String[] vcs = new String[dMaterialBuyList.size() + dDeviceBuyList.size()];

            for (int i = 0; i < dDeviceBuyList.size(); i++) {

                content[i][0] = dDeviceBuyList.get(i).getMbanktype();
                content[i][1] = dDeviceBuyList.get(i).getDBMidtype();
                content[i][2] = dDeviceBuyList.get(i).getDBUnit();
                content[i][3] = dDeviceBuyList.get(i).getDBCount() == null ? "0.00" : dDeviceBuyList.get(i).getDBCount().toString();
                content[i][4] = dDeviceBuyList.get(i).getDBPriceall() == null ? "0.00" : dDeviceBuyList.get(i).getDBPriceall().toPlainString();
            }
            for (int i = 0; i < dMaterialBuyList.size(); i++) {

                content[dDeviceBuyList.size() +i][0] = dMaterialBuyList.get(i).getMbanktype();
                content[dDeviceBuyList.size() +i][1] = dMaterialBuyList.get(i).getMBMidtype();
                content[dDeviceBuyList.size() +i][2] = dMaterialBuyList.get(i).getMBUnit();
                content[dDeviceBuyList.size() +i][3] = dMaterialBuyList.get(i).getMBCount() == null ? "0.00" : dMaterialBuyList.get(i).getMBCount().toPlainString();
                content[dDeviceBuyList.size() +i][4] = dMaterialBuyList.get(i).getMBPriceall() == null ? "0.00" : dMaterialBuyList.get(i).getMBPriceall().toPlainString();
            }

            HSSFWorkbook wb = getHSSFWorkbook(aPBase, sheetName, title, content, dDeviceBuyList.size(), null);
            wb.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 导出点位材料明细
     *
     * @param
     * @param
     * @throws IOException
     * @throws DocumentException
     */
    @RequestMapping("/exportsbypoints")
    public void exportsbypoints(String guid, String filename, FileOutputStream out) throws IOException, DocumentException {
        APBaseEntity aPBase = aPBaseService.queryObject(guid);

        Document document = new Document();
        BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        RtfWriter2.getInstance(document, out);
        Font titleFontye = new Font(bfChinese, 8, Font.BOLD);
        HeaderFooter footer = new HeaderFooter(new Phrase("页码：", titleFontye), true);
        footer.setAlignment(1);
        footer.setBorder(Rectangle.NO_BORDER);
        document.setFooter(footer);
        document.open();
        setexportspoint(document, bfChinese, aPBase);

        document.close();

    }


    /**
     * 导出点位材料明细【新】
     *
     * @param
     * @param
     * @throws IOException
     * @throws DocumentException
     */
    @RequestMapping("/exportsbypoint")
    public void exportsbypoint(String guid, FileOutputStream out) throws IOException, DocumentException {
        try {

            APBaseEntity aPBase = aPBaseService.queryObject(guid);
            Map<String, Object> mapss = new HashMap<String, Object>();
            String wheres = " 1=1 ";
            wheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
            mapss.put("filterSql", wheres + " ");
            List<APRangeEntity> aPRangeList = aPRangeService.queryList(mapss);

            String sheetName = "点位设备材料明细表";

            HSSFWorkbook wb = getNewHSSFWorkbook(aPBase, sheetName, aPRangeList, null);
            wb.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 材料设备购置表
     *
     * @param
     * @param
     * @throws IOException
     * @throws DocumentException
     */
    @RequestMapping("/exportsbymx")
    public void exportsbymx(String guid, FileOutputStream out) throws IOException, DocumentException {

        try {
            APBaseEntity aPBase = aPBaseService.queryObject(guid);
            List<DMaterialBuyEntity> dMaterialBuyList = dMaterialBuyService.queryListout(aPBase.getGuid());
            List<DDeviceBuyEntity> dDeviceBuyList = dDeviceBuyService.queryListout(aPBase.getGuid());

            String[] title = {"名称", "规格及型号", "单位", "数量", "单价(元)", "合价(元)"};
            String sheetName = "设备材料购置表";
            String[][] content = new String[dMaterialBuyList.size() + dDeviceBuyList.size()][title.length];
            String[] vcs = new String[dMaterialBuyList.size() + dDeviceBuyList.size()];
            for (int i = 0; i < dDeviceBuyList.size(); i++) {
                vcs[i] = "设备";
                content[i][0] = dDeviceBuyList.get(i).getDBMidtype();
                content[i][1] = dDeviceBuyList.get(i).getDBName();
                content[i][2] = dDeviceBuyList.get(i).getDBUnit();
                content[i][3] = dDeviceBuyList.get(i).getDBCount() == null ? "0.00" : dDeviceBuyList.get(i).getDBCount().toString();
                content[i][4] = dDeviceBuyList.get(i).getDBPrice() == null ? "0.00" : dDeviceBuyList.get(i).getDBPrice().toPlainString();
                content[i][5] = dDeviceBuyList.get(i).getDBPriceall() == null ? "0.00" : dDeviceBuyList.get(i).getDBPriceall().toPlainString();
            }

            for (int i = 0; i < dMaterialBuyList.size(); i++) {
                vcs[i] = "材料";
                content[dDeviceBuyList.size() + i][0] = dMaterialBuyList.get(i).getMBMidtype();
                content[dDeviceBuyList.size() + i][1] = dMaterialBuyList.get(i).getMBName();
                content[dDeviceBuyList.size() + i][2] = dMaterialBuyList.get(i).getMBUnit();
                content[dDeviceBuyList.size() + i][3] = dMaterialBuyList.get(i).getMBCount() == null ? "0.00" : dMaterialBuyList.get(i).getMBCount().toPlainString();
                content[dDeviceBuyList.size() + i][4] = dMaterialBuyList.get(i).getMBPrice() == null ? "0.00" : dMaterialBuyList.get(i).getMBPrice().toPlainString();
                content[dDeviceBuyList.size() + i][5] = dMaterialBuyList.get(i).getMBPriceall() == null ? "0.00" : dMaterialBuyList.get(i).getMBPriceall().toPlainString();
            }

            HSSFWorkbook wb = getHSSFWorkbook(aPBase, sheetName, title, content, dDeviceBuyList.size(), null);
            wb.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 材料设备拆除表
     *
     * @param
     * @param
     * @throws IOException
     * @throws DocumentException
     */
    @RequestMapping("/exportRemove")
    public void exportRemove(String guid, FileOutputStream out) throws IOException, DocumentException {

        try {
            APBaseEntity aPBase = aPBaseService.queryObject(guid);
            Map<String, Object> mapaPOther = new HashMap<>();

            //  拆除设备
            String dDeviceOldwheres = " 1=1 ";
            dDeviceOldwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
            dDeviceOldwheres += " and  d_r_datastate != '删除' ";
            mapaPOther.put("filterSql", dDeviceOldwheres + " ");
            List<DDeviceOldEntity> dDeviceOldList = dDeviceOldService.queryList(mapaPOther);
            List<DDeviceOldEntityOut> dDeviceOldOutList = new ArrayList<DDeviceOldEntityOut>();
            for (DDeviceOldEntity dDeviceOldEntity : dDeviceOldList) {
                DDeviceOldEntityOut dDeviceOldEntityOut = new DDeviceOldEntityOut();
                dDeviceOldEntityOut.setDRMidtype(dDeviceOldEntity.getDRMidtype());
                dDeviceOldEntityOut.setDRUnit("套");
                dDeviceOldEntityOut.setDRCount(new BigDecimal("1.000"));
                dDeviceOldEntityOut.setDRName(dDeviceOldEntity.getDRName());
                //  处置意见
                if (dDeviceOldEntity.getDROpinion() != null && dDeviceOldEntity.getDROpinion().equals("-9")) {
                    dDeviceOldEntityOut.setDROpinion("本项目利用");
                } else {
                    dDeviceOldEntityOut.setDROpinion(bDataDictionaryService.selectvaluebykey("opinion_type", dDeviceOldEntity.getDROpinion()));
                }
                dDeviceOldOutList.add(dDeviceOldEntityOut);
            }

            //  拆除材料
            mapaPOther.clear();
            String dMaterialOldwheres = " 1=1 ";
            dMaterialOldwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
            dMaterialOldwheres += " and  m_b_datastate != '删除' ";
            mapaPOther.put("filterSql", dMaterialOldwheres + " ");
            List<DMaterialOldEntity> dMaterialOldEntityList = dMaterialOldService.queryList(mapaPOther);
            List<DDeviceOldEntityOut> dMaterialOldOutList = new ArrayList<>();
            for (DMaterialOldEntity dMaterialOldEntity : dMaterialOldEntityList) {
                DDeviceOldEntityOut dDeviceOldEntityOut = new DDeviceOldEntityOut();
                dDeviceOldEntityOut.setDRMidtype(dMaterialOldEntity.getMBMidtype());
                dDeviceOldEntityOut.setDRName(dMaterialOldEntity.getMBName());
                dDeviceOldEntityOut.setDRUnit(dMaterialOldEntity.getMBUnit());
                dDeviceOldEntityOut.setDRCount(dMaterialOldEntity.getMBCount());
                dDeviceOldEntityOut.setDROpinion(bDataDictionaryService.selectvaluebykey("opinion_type", dMaterialOldEntity.getDROpinion()));
                dMaterialOldOutList.add(dDeviceOldEntityOut);
            }

            String[] title = {"名称", "规格及型号", "单位", "数量", "处置意见"};
//        String fileName = aPBase.getPBName() + "材料明细表" + System.currentTimeMillis() + ".xls";
            String sheetName = "设备材料拆除表";
            String[][] content = new String[dDeviceOldOutList.size() + dMaterialOldOutList.size()][title.length];
            String[] vcs = new String[dDeviceOldOutList.size() + dMaterialOldOutList.size()];
            for (int i = 0; i < dDeviceOldOutList.size(); i++) {

                content[i][0] = dDeviceOldOutList.get(i).getDRMidtype();
                content[i][1] = dDeviceOldOutList.get(i).getDRName();
                content[i][2] = dDeviceOldOutList.get(i).getDRUnit();
                content[i][3] = dDeviceOldOutList.get(i).getDRCount() == null ? "0.00" : dDeviceOldOutList.get(i).getDRCount().toString();
                content[i][4] = dDeviceOldOutList.get(i).getDROpinion();
            }

            for (int i = 0; i < dMaterialOldOutList.size(); i++) {

                content[dDeviceOldOutList.size() + i][0] = dMaterialOldOutList.get(i).getDRMidtype();
                content[dDeviceOldOutList.size() + i][1] = dMaterialOldOutList.get(i).getDRName();
                content[dDeviceOldOutList.size() + i][2] = dMaterialOldOutList.get(i).getDRUnit();
                content[dDeviceOldOutList.size() + i][3] = dMaterialOldOutList.get(i).getDRCount() == null ? "0.00" : dMaterialOldOutList.get(i).getDRCount().toPlainString();
                content[dDeviceOldOutList.size() + i][4] = dMaterialOldOutList.get(i).getDROpinion();
            }

            HSSFWorkbook wb = getHSSFWorkbook(aPBase, sheetName, title, content, dDeviceOldOutList.size(), null);
            wb.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static HSSFWorkbook getHSSFWorkbook(APBaseEntity aPBase, String sheetName,
                                               String[] title, String[][] values, int listSize, HSSFWorkbook wb) {

        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();

        // 表头样式
        style.setAlignment(HorizontalAlignment.CENTER); // 居中

        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框

        HSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setBold(true);
        font.setFontHeightInPoints((short) 10);//设置字体大小
        style.setFont(font);//选择需要用到的字体格式

        // 设置表格列宽
        sheet.setColumnWidth(0, 4000);
        sheet.setColumnWidth(1, 10000);
        sheet.setColumnWidth(2, 4000);
        sheet.setColumnWidth(3, 4000);
        sheet.setColumnWidth(4, 4000);

        // 内容单元格样式
        CellStyle style1 = wb.createCellStyle();
        style1.setAlignment(HorizontalAlignment.CENTER); // 居中
        style1.setBorderBottom(BorderStyle.THIN); //下边框
        style1.setBorderLeft(BorderStyle.THIN);//左边框
        style1.setBorderTop(BorderStyle.THIN);//上边框
        style1.setBorderRight(BorderStyle.THIN);//右边框

        //声明列对象
        HSSFCell cell = null;

        // 表头(合并单元格)
        cell = row.createCell(0);
        cell.setCellValue(aPBase.getPBName() + "_" + sheetName);
        cell.setCellStyle(style);
        CellRangeAddress region1 = null;
        if (sheetName.equals("材料汇总表") || sheetName.equals("设备材料拆除表")) {
            region1 = new CellRangeAddress(0, 0, 0, 4);
        } else {
            region1 = new CellRangeAddress(0, 0, 0, 5);
        }
        sheet.addMergedRegion(region1);

        //  主要设备行
        row = sheet.createRow(1);
        cell = row.createCell(0);
        cell.setCellValue("主要设备");
        cell.setCellStyle(style);
        CellRangeAddress region2 = null;
        if (sheetName.equals("材料汇总表") || sheetName.equals("设备材料拆除表")) {
            region2 = new CellRangeAddress(1, 1, 0, 4);
        } else {
            region2 = new CellRangeAddress(1, 1, 0, 5);
        }
        sheet.addMergedRegion(region2);
        setBorderForMergeCell(1, region2, sheet);

        //创建标题
        row = sheet.createRow(2);
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        boolean isTrue = true;  //  材料表头的循环
        boolean isFor = false;  //  材料内容的循环
        //创建内容
        for (int i = 0; i < values.length; i++) {

            if (i < listSize) {   //  先创建设备信息
                row = sheet.createRow(i + 3);
                for (int j = 0; j < values[i].length; j++) {
                    cell = row.createCell(j);
                    cell.setCellValue(values[i][j]);
                    cell.setCellStyle(style1);
                }

            } else {  //  再创建材料信息

                if (isTrue) { //  创建表头栏
                    row = sheet.createRow(i + 3);
                    cell = row.createCell(0);
                    cell.setCellValue("主要材料");
                    cell.setCellStyle(style);
                    CellRangeAddress region3 = null;
                    if (sheetName.equals("材料汇总表") || sheetName.equals("设备材料拆除表")) {
                        region3 = new CellRangeAddress(i + 3, i + 3, 0, 4);
                    } else {
                        region3 = new CellRangeAddress(i + 3, i + 3, 0, 5);
                    }
                    sheet.addMergedRegion(region3);
                    setBorderForMergeCell(1, region3, sheet);

                    row = sheet.createRow(i + 4);
                    for (int m = 0; m < title.length; m++) {    //  表头栏
                        cell = row.createCell(m);
                        cell.setCellValue(title[m]);
                        cell.setCellStyle(style);
                    }

                    isTrue = false;
                    isFor = true;
                }
                if (isFor) {
                    row = sheet.createRow(i + 5);
                    for (int j = 0; j < values[i].length; j++) {
                        cell = row.createCell(j);
                        cell.setCellValue(values[i][j]);
                        cell.setCellStyle(style1);
                    }
                }
            }

        }
        return wb;
    }


    public HSSFWorkbook getNewHSSFWorkbook(APBaseEntity aPBase, String sheetName, List<APRangeEntity> aPRangeList, HSSFWorkbook wb) {

        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }



        for (APRangeEntity aPRangeEntity : aPRangeList) {



            // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet(aPRangeEntity.getPRName()+sheetName);

            // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
            HSSFRow row = sheet.createRow(0);

            // 第四步，创建单元格，并设置值表头 设置表头居中
            HSSFCellStyle style = wb.createCellStyle();

            // 表头样式
            style.setAlignment(HorizontalAlignment.CENTER);         // 水平居中
            style.setVerticalAlignment(VerticalAlignment.CENTER);   // 垂直居中
            style.setBorderBottom(BorderStyle.THIN); //下边框
            style.setBorderLeft(BorderStyle.THIN);//左边框
            style.setBorderTop(BorderStyle.THIN);//上边框
            style.setBorderRight(BorderStyle.THIN);//右边框

            //  表头字体
            HSSFFont font = wb.createFont();
            font.setFontName("宋体");
            font.setBold(true);
            font.setFontHeightInPoints((short) 10);//设置字体大小
            style.setFont(font);//选择需要用到的字体格式

            //  正文字体
            HSSFFont font1 = wb.createFont();
            font1.setFontName("宋体");
            font1.setBold(false);
            font1.setFontHeightInPoints((short) 10);//设置字体大小

            // 设置表格列宽
            sheet.setColumnWidth(0, 4000);
            sheet.setColumnWidth(1, 4000);
            sheet.setColumnWidth(2, 6000);
            sheet.setColumnWidth(3, 10000);
            sheet.setColumnWidth(4, 20000);
            sheet.setColumnWidth(5, 2000);
            sheet.setColumnWidth(6, 3000);

            // 内容单元格样式
            CellStyle style1 = wb.createCellStyle();
            style1.setAlignment(HorizontalAlignment.CENTER);            // 水平居中
            style1.setVerticalAlignment(VerticalAlignment.CENTER);      // 垂直居中
            style1.setBorderBottom(BorderStyle.THIN);                   //下边框
            style1.setBorderLeft(BorderStyle.THIN);                     //左边框
            style1.setBorderTop(BorderStyle.THIN);                      //上边框
            style1.setBorderRight(BorderStyle.THIN);                    //右边框
            style1.setFont(font1);

            //声明列对象
            HSSFCell cell = null;

            //  标题栏
            cell = row.createCell(0);
            cell.setCellValue(aPBase.getPBName() + "点位设备材料明细");
            cell.setCellStyle(style);
            CellRangeAddress region1 = new CellRangeAddress(0, 0, 0, 6);
            sheet.addMergedRegion(region1);
//        setBorderForMergeCell(1,region1,sheet);


            String[] title = {"杆塔点位", "新增类型", "类别", "名称", "规格型号", "单位", "数量"};




            //  项目范围栏
            row = sheet.createRow(1);
            cell = row.createCell(0);
            cell.setCellValue(aPRangeEntity.getPRName() + "点位设备材料明细");
            cell.setCellStyle(style);
            CellRangeAddress region2 = new CellRangeAddress(1, 1, 0, 6);
            sheet.addMergedRegion(region2);
            setBorderForMergeCell(1, region2, sheet);

            //  表头栏
            row = sheet.createRow(2);
            for (int i = 0; i < title.length; i++) {
                cell = row.createCell(i);
                cell.setCellValue(title[i]);
                cell.setCellStyle(style);
            }

            Map<String, Object> mapapole = new HashMap<>();
            String polewheres = " 1=1 ";
            polewheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
            polewheres += " and  p_r_guid = '" + aPRangeEntity.getGuid() + "' ";
            polewheres += " and  p_p_datastate!= '删除' ";
            mapapole.put("filterSql", polewheres + " order by p_r_guid,   CONVERT(Right(p_p_code,LENGTH(p_p_code)-instr(p_p_code, '#')) ,SIGNED) asc ");


            int k = 0;  //  起始行
            int j = 2;  //  结束行
            int m = 0;  //  没有点位工作内容的行数
            boolean isTrue = true;
            List<DPointEntity> dPointList = dPointService.queryList(mapapole);
            for (DPointEntity dPointEntity : dPointList) {
                List<ReOutMeterAndDev> ReOutMeterAndDevlist = dWorkContentService.getMeterAndDevByPoint(dPointEntity.getGuid());

                if (ReOutMeterAndDevlist.size() > 0) { //  点位有工作内容时

                    for (int i = 0; i < ReOutMeterAndDevlist.size(); i++) {
                        if (i == 0) {
                            k = j + 1 ;
                        }
                        row = sheet.createRow(j + 1 + m);
                        cell = row.createCell(0);
                        cell.setCellValue(dPointEntity.getPPCode() + dPointEntity.getPPName());
                        cell.setCellStyle(style1);

                        cell = row.createCell(1);
                        cell.setCellValue(ReOutMeterAndDevlist.get(i).getWorktype());
                        cell.setCellStyle(style1);

                        cell = row.createCell(2);
                        cell.setCellValue(ReOutMeterAndDevlist.get(i).getMidtype());
                        cell.setCellStyle(style1);

                        cell = row.createCell(3);
                        cell.setCellValue(ReOutMeterAndDevlist.get(i).getMname());
                        cell.setCellStyle(style1);

                        cell = row.createCell(4);
                        cell.setCellValue(ReOutMeterAndDevlist.get(i).getParameter());
                        cell.setCellStyle(style1);

                        cell = row.createCell(5);
                        cell.setCellValue(ReOutMeterAndDevlist.get(i).getUnit());
                        cell.setCellStyle(style1);

                        cell = row.createCell(6);
                        cell.setCellValue(ReOutMeterAndDevlist.get(i).getCounts());
                        cell.setCellStyle(style1);


                        j++;


                    }

                    //  合并 杆塔点位列
                    if(k+m != j+m){
                        CellRangeAddress region3 = new CellRangeAddress(k + m, j + m, 0, 0);
                        sheet.addMergedRegion(region3);
                        setBorderForMergeCell(1, region3, sheet);
                    }

                } else {  //  点位无工作内容时
                    row = sheet.createRow(j + 1 + m);
                    cell = row.createCell(0);
                    cell.setCellValue(dPointEntity.getPPCode() + dPointEntity.getPPName());
                    cell.setCellStyle(style1);

                    cell = row.createCell(1);
                    cell.setCellValue("");
                    cell.setCellStyle(style);

                    cell = row.createCell(2);
                    cell.setCellValue("");
                    cell.setCellStyle(style);

                    cell = row.createCell(3);
                    cell.setCellValue("");
                    cell.setCellStyle(style);

                    cell = row.createCell(4);
                    cell.setCellValue("");
                    cell.setCellStyle(style);

                    cell = row.createCell(5);
                    cell.setCellValue("");
                    cell.setCellStyle(style);

                    cell = row.createCell(6);
                    cell.setCellValue("");
                    cell.setCellStyle(style);

                    m++;
                }
            }
        }

        return wb;
    }

    /**
     * 合并单元格设置边框
     *
     * @param i
     * @param cellRangeTitle
     * @param sheet
     * @param
     */
    private static void setBorderForMergeCell(int i, CellRangeAddress cellRangeTitle, HSSFSheet sheet) {
        RegionUtil.setBorderBottom(i, cellRangeTitle, sheet);
        RegionUtil.setBorderLeft(i, cellRangeTitle, sheet);
        RegionUtil.setBorderRight(i, cellRangeTitle, sheet);
        RegionUtil.setBorderTop(i, cellRangeTitle, sheet);
    }

    public void setexportspoint(Document document, BaseFont bfChinese, APBaseEntity aPBase) throws DocumentException {

        //表题字体
        Font titleFont = new Font(bfChinese, 11, Font.BOLD);
        //表格字体
        Font contextFont = new Font(bfChinese, 8, Font.NORMAL);
        // 签字字体
        Font qinFont = new Font(bfChinese, 10, Font.NORMAL);
        Paragraph title = new Paragraph(aPBase.getPBName() + "点位材料明细");
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(titleFont);
        document.add(title);
        document.add(new Paragraph(""));
        Map<String, Object> mapss = new HashMap<String, Object>();
        String wheres = " 1=1 ";
        wheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        mapss.put("filterSql", wheres + " ");
        List<APRangeEntity> aPRangeList = aPRangeService.queryList(mapss);
        for (APRangeEntity aPRangeEntity : aPRangeList) {
            Paragraph mytitle = new Paragraph(aPRangeEntity.getPRName() + "点位材料明细");
            mytitle.setAlignment(Element.ALIGN_CENTER);
            mytitle.setFont(contextFont);
            document.add(mytitle);

            Map<String, Object> mapapole = new HashMap<>();
            String polewheres = " 1=1 ";
            polewheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
            polewheres += " and  p_r_guid = '" + aPRangeEntity.getGuid() + "' ";
            polewheres += " and  p_p_datastate!= '删除' ";
            mapapole.put("filterSql", polewheres + " ");
            List<DPointEntity> dPointList = dPointService.queryList(mapapole);
            Table table = new Table(7);
            int[] withs = {1, 2, 2, 3, 3, 1, 1};
            table.setWidths(withs);
            /** 表格所占页面宽度 author:yyli Sep 15, 2010 */
            table.setWidth(90);
            /** 居中显示 author:yyli Sep 15, 2010 */
            table.setAlignment(Element.ALIGN_CENTER);
            /** 自动填满 author:yyli Sep 15, 2010 */
            table.setAutoFillEmptyCells(true);
            OutPutUtils.addcell(table, "杆塔点位", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "新增类型", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "类别", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "名称", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "规格型号", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "单位", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "数量", 0, 0, true, contextFont);
            for (DPointEntity dPointEntity : dPointList) {
                List<ReOutMeterAndDev> ReOutMeterAndDevlist = dWorkContentService.getMeterAndDevByPoint(dPointEntity.getGuid());
                OutPutUtils.addcell(table, dPointEntity.getPPCode() + dPointEntity.getPPName(), ReOutMeterAndDevlist.size(), 0, true, contextFont);
                if (ReOutMeterAndDevlist.size() > 0) {
                    for (ReOutMeterAndDev reOutMeterAndDev : ReOutMeterAndDevlist) {
                        OutPutUtils.addcell(table, reOutMeterAndDev.getWorktype(), 0, 0, true, contextFont);
                        OutPutUtils.addcell(table, reOutMeterAndDev.getMidtype(), 0, 0, true, contextFont);
                        OutPutUtils.addcell(table, reOutMeterAndDev.getMname(), 0, 0, true, contextFont);
                        OutPutUtils.addcell(table, reOutMeterAndDev.getParameter(), 0, 0, true, contextFont);
                        OutPutUtils.addcell(table, reOutMeterAndDev.getUnit(), 0, 0, true, contextFont);
                        OutPutUtils.addcell(table, reOutMeterAndDev.getCounts() + "", 0, 0, true, contextFont);
                    }
                } else {
                    OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
                    OutPutUtils.addcell(table, "", 0, 0, true, contextFont);

                }

            }
            document.add(table);
            document.add(new Paragraph(""));
        }

    }


    public void setexports(Document document, BaseFont bfChinese, APBaseEntity aPBase) throws DocumentException {
        Map<String, Object> mapaPOther = new HashMap<>();

        String mapaPOtherwheres = " 1=1 ";
        mapaPOtherwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        mapaPOtherwheres += " and  p_o_datastate != '删除' ";
        mapaPOther.put("filterSql", mapaPOtherwheres + " ");
        List<APOtherEntity> aPOtherList = aPOtherService.queryList(mapaPOther);
        APOtherEntity aPOther = aPOtherList.get(0);
        SysDeptEntity sysDeptEntity = sysDeptService.queryObject(aPBase.getPBOrgid());
        mapaPOther.clear();
        String aPCriteriawheres = " 1=1 ";
        aPCriteriawheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        aPCriteriawheres += " and  p_c_datastate != '删除' ";
        mapaPOther.put("filterSql", aPCriteriawheres + " ");
        List<APCriteriaEntity> aPCriteriaList = aPCriteriaService.queryList(mapaPOther);
        String aPCriteria1 = "";
        String aPCriteria2 = "";
        String aPCriteria3 = "";
        for (APCriteriaEntity aPCriteriaEntity : aPCriteriaList) {
            if (!aPCriteria1.contains(aPCriteriaEntity.getPCName())) {
                aPCriteria1 += aPCriteriaEntity.getPCName() + "；";
            }
            if (!aPCriteria2.contains(aPCriteriaEntity.getPCChapter())) {
                aPCriteria2 += aPCriteriaEntity.getPCChapter() + "；";
            }
            if (!aPCriteria3.contains(aPCriteriaEntity.getPCCriteria())) {
                aPCriteria3 += aPCriteriaEntity.getPCCriteria() + "";
            }
        }
        mapaPOther.clear();
        String aPEstimatewheres = " 1=1 ";
        aPEstimatewheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        aPEstimatewheres += " and  p_e_datastate != '删除' ";
        mapaPOther.put("filterSql", aPEstimatewheres + " ");
        List<APEstimateEntity> APEstimateEntitylist = aPEstimateService.queryList(mapaPOther);
        //总费用
        BigDecimal tole = BigDecimal.ZERO;
        //建筑工程费
        BigDecimal tole1 = BigDecimal.ZERO;
        //安装费
        BigDecimal tole2 = BigDecimal.ZERO;
        //购置费
        BigDecimal tole3 = BigDecimal.ZERO;
        //其他费用
        BigDecimal tole4 = BigDecimal.ZERO;
        for (APEstimateEntity aPEstimateEntity : APEstimateEntitylist) {
            BigDecimal tol = aPEstimateEntity.getPEFinal() == null ? BigDecimal.ZERO : aPEstimateEntity.getPEFinal();
            tole = tole.add(tol);

            if (PriceConfig.otherPrice.contains(aPEstimateEntity.getPEItem())) {
                tole4 = tole4.add(tol);
            } else if (PriceConfig.buildPrice.contains(aPEstimateEntity.getPEItem())) {
                tole1 = tole1.add(tol);
            } else if (PriceConfig.buyPrice.contains(aPEstimateEntity.getPEItem())) {
                tole3 = tole3.add(tol);
            } else {
                tole2 = tole2.add(tol);
            }
        }

        mapaPOther.clear();
        String aPCostwheres = " 1=1 ";
        aPCostwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        aPCostwheres += " and  p_c_datastate != '删除' ";
        mapaPOther.put("filterSql", aPCostwheres + " ");
        List<APCostEntity> aPCostList = aPCostService.queryList(mapaPOther);

        mapaPOther.clear();
        String dDeviceOldwheres = " 1=1 ";
        dDeviceOldwheres += " and  p_guid = '" + aPBase.getGuid() + "' ";
        dDeviceOldwheres += " and  d_r_datastate != '删除' ";
        mapaPOther.put("filterSql", dDeviceOldwheres + " ");
        List<DDeviceOldEntity> dDeviceOldList = dDeviceOldService.queryList(mapaPOther);

        List<DDeviceBuyEntity> dDeviceBuyList = dDeviceBuyService.queryListout(aPBase.getGuid());

        List<DMaterialBuyEntity> dMaterialBuyList = dMaterialBuyService.queryListout(aPBase.getGuid());


        //表题字体
        Font titleFont = new Font(bfChinese, 11, Font.BOLD);
        //表格字体
        Font contextFont = new Font(bfChinese, 8, Font.NORMAL);
        // 签字字体
        Font qinFont = new Font(bfChinese, 10, Font.NORMAL);
        Paragraph title = new Paragraph("配网技改/修理项目申请书（适用于总投资100万元以下的配网技改/修理项目）");
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(titleFont);
        document.add(title);

        Table table = new Table(13);
        int[] withs = {1, 3, 3, 2, 2, 3, 3, 2, 2, 2, 2, 2, 4};
        table.setWidths(withs);
        /** 表格所占页面宽度 author:yyli Sep 15, 2010 */
        table.setWidth(90);
        /** 居中显示 author:yyli Sep 15, 2010 */
        table.setAlignment(Element.ALIGN_CENTER);
        /** 自动填满 author:yyli Sep 15, 2010 */
        table.setAutoFillEmptyCells(true);

        OutPutUtils.addcell(table, "项目名称", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, aPBase.getPBName(), 0, 10, false, contextFont);
        OutPutUtils.addcell(table, "项目单位", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, sysDeptEntity.getName(), 0, 7, false, contextFont);
        OutPutUtils.addcell(table, "项目实施年度", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPOYear(), 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "项目内容", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPOContent(), 0, 10, false, contextFont);
        OutPutUtils.addcell(table, "是否反措项目", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPOIsstep(), 0, 7, false, contextFont);
        OutPutUtils.addcell(table, "是否规划项目", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPOIsplan(), 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "重点投入方向", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPODirection(), 0, 10, false, contextFont);
        //动态跨行
        OutPutUtils.addcell(table, "必要性", 5 + dDeviceOldList.size() + 1 + 7 + 7 + 7 + 1, 0, true, contextFont);
        OutPutUtils.addcell(table, "预算标准", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "依据《南方电网生产项目准入及预算标准》:" + aPCriteria1, 0, 10, false, contextFont);
        OutPutUtils.addcell(table, "准入条件", 2, 0, true, contextFont);
        OutPutUtils.addcell(table, "对应条款", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "依据《南方电网生产项目准入及预算标准》:" + aPCriteria2, 0, 10, false, contextFont);
        OutPutUtils.addcell(table, "条款详述", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "依据《南方电网生产项目准入及预算标准》:" + aPCriteria3, 0, 10, false, contextFont);
        OutPutUtils.addcell(table, "设备（设施）基本情况", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPODevice(), 0, 10, false, contextFont);
        OutPutUtils.addcell(table, "存在问题", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPOProblem(), 0, 10, false, contextFont);
        OutPutUtils.addcell(table, "一、改造/检修/拆除设备清单（项目主要改造/检修/拆除的资产级设备明细）", 0, 12, false, contextFont);
        OutPutUtils.addcell(table, "设备名称", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "设备编号", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "设备厂家", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "设备型号", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "出厂日期", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "投运日期", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "固定资产卡片编号", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "资产原值（万元）", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "资产净值（万元）", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "资产净值率", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "拟拆除日期", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "处置意见", 0, 0, true, contextFont);


        for (DDeviceOldEntity dDeviceOldEntity : dDeviceOldList) {
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRName(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRFactory(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRParameter(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRFactorydate() == null ? "" : DateUtils.format(dDeviceOldEntity.getDRFactorydate(), "yyyy-MM-dd"), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRDeliverydate() == null ? "" : DateUtils.format(dDeviceOldEntity.getDRDeliverydate(), "yyyy-MM-dd"), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRCode(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRPrice() == null ? "" : dDeviceOldEntity.getDRPrice().toPlainString(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRNetassets() == null ? "" : dDeviceOldEntity.getDRNetassets().toPlainString(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRRate() == null ? "" : dDeviceOldEntity.getDRRate().toPlainString(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceOldEntity.getDRRemovedate() == null ? "" : DateUtils.format(dDeviceOldEntity.getDRRemovedate(), "yyyy-MM-dd"), 0, 0, true, contextFont);
            if (dDeviceOldEntity.getDROpinion() != null && dDeviceOldEntity.getDROpinion().equals("-9")) {
                OutPutUtils.addcell(table, "本项目利用", 0, 0, true, contextFont);
            } else {
                OutPutUtils.addcell(table, bDataDictionaryService.selectvaluebykey("opinion_type", dDeviceOldEntity.getDROpinion()), 0, 0, true, contextFont);
            }
        }

        OutPutUtils.addcell(table, "二、缺陷（故障）记录", 0, 12, false, contextFont);
        OutPutUtils.addcell(table, "序号", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "缺陷编号", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "缺陷类别", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "严重等级", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "缺陷表象", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "发现人描述", 0, 4, true, contextFont);
        OutPutUtils.addcell(table, "发现人意见", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "发现时间", 0, 0, true, contextFont);

        for (int i = 0; i < 5; i++) {
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 4, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 2, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
        }

        OutPutUtils.addcell(table, "三、设备状态评价与风险评估（改造设备状态评价与风险评估结果）", 0, 12, false, contextFont);
        OutPutUtils.addcell(table, "年份", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "设备", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "状态", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "风险等级", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "扣分点", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "扣分值", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "重要度评价", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "运维等级", 0, 0, true, contextFont);

        for (int i = 0; i < 5; i++) {
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 2, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 2, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 2, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 2, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
        }
        OutPutUtils.addcell(table, "四、关联生产项目历史（历史开展过的技改、大修项目，日常维修项目需列出近三年费用情况））", 0, 12, false, contextFont);
        OutPutUtils.addcell(table, "立项年份", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "项目类型", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "项目编码", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "项目名称", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, "项目总投资（万元）", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "项目简要内容", 0, 3, true, contextFont);


        for (int i = 0; i < 5; i++) {
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 0, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 2, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 3, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 2, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 3, true, contextFont);
        }
        OutPutUtils.addcell(table, "项目方案", 4, 0, true, contextFont);

        OutPutUtils.addcell(table, "实施必要性", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPOMust(), 0, 10, false, contextFont);

        //动态跨行
        OutPutUtils.addcell(table, "技术实施方案", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPOSkill(), 0, 10, false, contextFont);
        OutPutUtils.addcell(table, "停电施工方案\n（临时过渡方案）", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPOPower(), 0, 10, false, contextFont);
        OutPutUtils.addcell(table, "对环境的影响及经济或社会效益预测", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, aPOther.getPOEffect(), 0, 10, false, contextFont);

        //动态跨行
        OutPutUtils.addcell(table, "项目造价", 4 + 2 + aPCostList.size() + dDeviceBuyList.size() + 2 + dMaterialBuyList.size() + 2, 0, true, contextFont);
        OutPutUtils.addcell(table, "年度计划资金分解（万元）", 2, 3, true, contextFont);
        OutPutUtils.addcell(table, "项目总投资", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, tole.toPlainString(), 0, 7, true, contextFont);
        OutPutUtils.addcell(table, "____年计划投资", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "", 0, 1, true, contextFont);
        OutPutUtils.addcell(table, "____年计划投资", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "", 0, 1, true, contextFont);
        OutPutUtils.addcell(table, "____年计划投资", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "", 0, 1, true, contextFont);

        OutPutUtils.addcell(table, "费用组成（万元）", 2, 3, true, contextFont);
        OutPutUtils.addcell(table, "建筑工程费", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, "安装工程费（包含调试费）", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, "设备购置费", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "其他费用", 0, 1, true, contextFont);
        OutPutUtils.addcell(table, tole1.toPlainString(), 0, 3, true, contextFont);
        OutPutUtils.addcell(table, tole2.toPlainString(), 0, 3, true, contextFont);
        OutPutUtils.addcell(table, tole3.toPlainString(), 0, 2, true, contextFont);
        OutPutUtils.addcell(table, tole4.toPlainString(), 0, 1, true, contextFont);

        OutPutUtils.addcell(table, "其他费用(可以不限于以下其他费用的明细，但需要填写清楚取费依据和说明。)", 0, 12, true, contextFont);
        OutPutUtils.addcell(table, "序号", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "其他费用明细", 0, 4, true, contextFont);
        OutPutUtils.addcell(table, "计取依据及说明", 0, 4, true, contextFont);
        OutPutUtils.addcell(table, "合价（万元）", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "备注", 0, 0, true, contextFont);
        for (APCostEntity aPCostEntity : aPCostList) {
            OutPutUtils.addcell(table, aPCostEntity.getPCNum(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, aPCostEntity.getPCDetail(), 0, 4, true, contextFont);
            OutPutUtils.addcell(table, aPCostEntity.getPCExplain(), 0, 4, false, contextFont);
            OutPutUtils.addcell(table, aPCostEntity.getPCPrice() == null ? "0.00" : aPCostEntity.getPCPrice().toPlainString(), 0, 2, true, contextFont);
            OutPutUtils.addcell(table, aPCostEntity.getPCRemarks(), 0, 0, true, contextFont);
        }
        OutPutUtils.addcell(table, "主要设备", 0, 12, true, contextFont);
        OutPutUtils.addcell(table, "名称", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, "规格及型号", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, "单位", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "数量", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "单价（元）", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "合价（元）", 0, 0, true, contextFont);
        for (DDeviceBuyEntity dDeviceBuyEntity : dDeviceBuyList) {

            OutPutUtils.addcell(table, dDeviceBuyEntity.getDBMidtype(), 0, 3, true, contextFont);
            OutPutUtils.addcell(table, dDeviceBuyEntity.getDBName(), 0, 3, true, contextFont);
            OutPutUtils.addcell(table, dDeviceBuyEntity.getDBUnit(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dDeviceBuyEntity.getDBCount() == null ? "0" : dDeviceBuyEntity.getDBCount().toString(), 0, 2, true, contextFont);
            OutPutUtils.addcell(table, dDeviceBuyEntity.getDBPrice() == null ? "0.00" : dDeviceBuyEntity.getDBPrice().toPlainString(), 0, 2, true, contextFont);
            OutPutUtils.addcell(table, dDeviceBuyEntity.getDBPriceall() == null ? "0.00" : dDeviceBuyEntity.getDBPriceall().toPlainString(), 0, 0, true, contextFont);
        }
        OutPutUtils.addcell(table, "主要材料", 0, 12, true, contextFont);
        OutPutUtils.addcell(table, "名称", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, "规格及型号", 0, 3, true, contextFont);
        OutPutUtils.addcell(table, "单位", 0, 0, true, contextFont);
        OutPutUtils.addcell(table, "数量", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "单价（元）", 0, 2, true, contextFont);
        OutPutUtils.addcell(table, "合价（元）", 0, 0, true, contextFont);
        for (DMaterialBuyEntity dMaterialBuyEntity : dMaterialBuyList) {
            OutPutUtils.addcell(table, dMaterialBuyEntity.getMBMidtype(), 0, 3, true, contextFont);
            OutPutUtils.addcell(table, dMaterialBuyEntity.getMBName(), 0, 3, true, contextFont);
            OutPutUtils.addcell(table, dMaterialBuyEntity.getMBUnit(), 0, 0, true, contextFont);
            OutPutUtils.addcell(table, dMaterialBuyEntity.getMBCount() == null ? "0.00" : dMaterialBuyEntity.getMBCount().toPlainString(), 0, 2, true, contextFont);
            OutPutUtils.addcell(table, dMaterialBuyEntity.getMBPrice() == null ? "0.00" : dMaterialBuyEntity.getMBPrice().toPlainString(), 0, 2, true, contextFont);
            OutPutUtils.addcell(table, dMaterialBuyEntity.getMBPriceall() == null ? "0.00" : dMaterialBuyEntity.getMBPriceall().toPlainString(), 0, 0, true, contextFont);
        }
        OutPutUtils.addcell(table, "项目实施周期（月）或完成时间", 0, 13, true, contextFont);
        OutPutUtils.addcell(table, "序号", 0, 4, true, contextFont);
        OutPutUtils.addcell(table, "时间", 0, 5, true, contextFont);
        OutPutUtils.addcell(table, "事件", 0, 4, true, contextFont);
        for (int i = 0; i < 5; i++) {
            OutPutUtils.addcell(table, "", 0, 4, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 5, true, contextFont);
            OutPutUtils.addcell(table, "", 0, 4, true, contextFont);
        }
        OutPutUtils.addcell(table, "主要附图（另附图纸）", 0, 13, true, contextFont);

        document.add(table);
        Paragraph titles = new Paragraph("      编制：                               审核：                               批准：                ");
        titles.setAlignment(Element.ALIGN_CENTER);
        titles.setFont(qinFont);
        document.add(titles);
    }


    //删除本地文件及目录
    public boolean delFile(File file) {
        if (!file.exists()) {
            return false;
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                delFile(f);
            }
        }
        return file.delete();
    }


}
